package me.jiatao.jetpack.paging;

import android.arch.paging.PagedListAdapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.util.DiffUtil;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by JiaTao on 2018/10/12.
 * Email: 535318720@qq.com
 * Version: 1.0
 * Description:
 */
public class MyAdapter extends PagedListAdapter<DataBean, MyViewHolder> {

    public static MyAdapter getInstance(Context context) {
        DiffUtil.ItemCallback<DataBean> mDiffCallback = new DiffUtil.ItemCallback<DataBean>() {
            @Override
            public boolean areItemsTheSame(@NonNull DataBean oldItem, @NonNull DataBean newItem) {
                Log.d("DiffCallback", "areItemsTheSame");
                return oldItem.id == newItem.id;
            }

            @Override
            public boolean areContentsTheSame(@NonNull DataBean oldItem, @NonNull DataBean newItem) {
                Log.d("DiffCallback", "areContentsTheSame");
                return (oldItem == newItem);
            }
        };

        MyAdapter myAdapter = new MyAdapter(mDiffCallback, context);
        return myAdapter;
    }

    private Context mContext;

    public MyAdapter(DiffUtil.ItemCallback<DataBean> mDiffCallback, Context context) {
        super(mDiffCallback);
        this.mContext = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext.getApplicationContext()).inflate(android.R.layout.simple_list_item_2, null);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        DataBean data = getItem(position);
        holder.text1.setText(String.valueOf(position));
        holder.text2.setText(String.valueOf(data.content));
    }
}